package com.dqhl.community.view;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.util.ViewHolder;

import java.util.List;

/**
 * Created by cheng on 2016/9/5.
 * 物流公司选择 popupwindow
 */
public class ExpressSelectPopupWindow extends PopupWindow {

    private Activity context;
    private ListView lvExpress;
    private TextView tvFilterBg;

    public ExpressSelectPopupWindow(Activity context, List<String> expressList, AdapterView.OnItemClickListener listener) {
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.popupwindow_express_select, null);
        tvFilterBg = (TextView) view.findViewById(R.id.tv_filter_bg);
        lvExpress = (ListView) view.findViewById(R.id.lv_express);
        MyAdapter myAdapter = new MyAdapter(expressList);
        lvExpress.setAdapter(myAdapter);
        lvExpress.setOnItemClickListener(listener);

        tvFilterBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        setBackgroundDrawable(new ColorDrawable(0x70000000));
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setFocusable(true);
        setOutsideTouchable(true);
        setContentView(view);
        update();
    }

    class MyAdapter extends BaseAdapter {

        private List<String> list;

        public MyAdapter(List<String> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = LayoutInflater.from(context).inflate(R.layout.lv_item_express, viewGroup, false);
            }

            TextView tvExpress = ViewHolder.get(view, R.id.tv_express);
            tvExpress.setText(list.get(i));
            return view;
        }
    }
}
